package com.feige.exceptionhandler;

import com.feige.result.AjaxResult;
import com.feige.utils.exception.BaseException;
import com.feige.utils.exception.CustomException;
import com.feige.utils.exception.user.CaptchaException;
import com.feige.utils.exception.user.CaptchaExpireException;
import com.feige.utils.exception.user.UserDisableException;
import com.feige.utils.exception.user.UserPasswordNotMatchException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Objects;

/**
 * @author feige
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public AjaxResult error(Exception e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(500,e.getMessage());
    }


    /**
     * //特定异常   先找特定异常  再找全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public AjaxResult error(ArithmeticException e) {
        log.error(e.getMessage(), e);
        return AjaxResult.error(500,e.getMessage());
    }

    @ExceptionHandler(UserPasswordNotMatchException.class)
    public AjaxResult error(UserPasswordNotMatchException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(CaptchaException.class)
    public AjaxResult error(CaptchaException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(CaptchaExpireException.class)
    public AjaxResult error(CaptchaExpireException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    public AjaxResult error(CustomException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(BaseException.class)
    public AjaxResult error(BaseException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getCode(),e.getMessage());
    }

    @ExceptionHandler(UserDisableException.class)
    public AjaxResult error(UserDisableException e){
        log.error(e.getMessage(), e);
        return AjaxResult.error(e.getCode(),e.getMessage());
    }
    @ExceptionHandler(BindException.class)
    public AjaxResult error(BindException e){
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return AjaxResult.error(500,message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AjaxResult error(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
        return AjaxResult.error(500,message);
    }
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public AjaxResult error(SQLIntegrityConstraintViolationException e) {
        log.error(e.getMessage(), e);
        return AjaxResult.error(500,"插入或更新的数据和数据库中的数据冲突，部分数据是唯一的，不能重复！");
    }
}
